package com.zabra.zl.ui.index_fragment.deal;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpFragment;
import com.zabra.zl.data.AllocationManageBean;
import com.zabra.zl.data.EventMessage;
import com.zabra.zl.myview.state_layout.OtherView;
import com.zabra.zl.ui.detailsallocatin.DetailsAllocationActivity;
import com.zabra.zl.ui.godeal.GoDealActivity;
import com.zabra.zl.ui.index_fragment.deal.contract.DealFragmentContract;
import com.zabra.zl.ui.index_fragment.deal.presenter.DealFragmentPresenter;
import com.zabra.zl.ui.nullpage.NullPersonActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.SharedPreferencesUtil;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class DealFragment extends BaseMvpFragment<DealFragmentPresenter> implements DealFragmentContract.View {
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.otherview)
    OtherView otherview;
    @BindView(R.id.refresh)
    PullToRefreshLayout refresh;
    private List<AllocationManageBean.RespDataBean.DataBean> list = new ArrayList<>();
    private Adp adp;
    private int page = 0;
    private Dialog dialog;
    private int orderId;
    private Bundle bundle = new Bundle();

    @Override
    protected void initView(View view) {
        otherview.setHolder(mHolder);
        mPresenter = new DealFragmentPresenter();
        mPresenter.attachView(this);
        mPresenter.getData("1", "", page + "", Config.PAGESIZE);
        refresh.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                page = 0;
                mPresenter.getDataFresh("1", "", page + "", Config.PAGESIZE);
            }

            @Override
            public void loadMore() {
                page++;
                mPresenter.getDataLoadMore("1", "", page + "", Config.PAGESIZE);
            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_deal;
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(getActivity());
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
        refresh.finishRefresh();
        refresh.finishLoadMore();
    }

    @Override
    public void onSuccess(AllocationManageBean bean) {
        initData(bean);
    }

    @Override
    public void onSuccessFresh(AllocationManageBean bean) {
        refresh.finishRefresh();
        initData(bean);
    }

    @Override
    public void onSuccessLoadMore(AllocationManageBean bean) {
        refresh.finishLoadMore();
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            list.addAll(bean.getRespData().getData());
            //延迟更新数据，避免卡顿
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    adp.notifyItemRangeChanged(0, bean.getRespData().getData().size());
                }
            }, 500);
            if (bean.getRespData().getData().size() == 0) {
                ToastUtil.showToast(getActivity(), "没有更多数据");
            }
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(getActivity());
        } else {
            ToastUtil.showToast(getActivity(), bean.getRespDesc());
        }
    }

    private void initData(AllocationManageBean bean) {
        list.clear();
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            list.addAll(bean.getRespData().getData());
            if (list.size() == 0) {
                otherview.showEmptyView();
                refresh.setCanLoadMore(false);
            } else {
                refresh.setCanLoadMore(true);
                otherview.showContentView();
            }
            recycler.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
            recycler.setNestedScrollingEnabled(false);
            adp = new Adp(R.layout.item_deal, list);
            recycler.setAdapter(adp);
            adp.openLoadAnimation();
            adp.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    orderId = list.get(position).getBpOrderId();
                    bundle.putInt("orderid", orderId);
                    toClass(view.getContext(), GoDealActivity.class, bundle);
                }
            });
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(getActivity());
        } else {
            ToastUtil.showToast(getActivity(), bean.getRespDesc());
        }
    }

}
